//
//  MyActivityViewController.m
//  豆瓣2
//
//  Created by dllo on 15/11/7.
//  Copyright (c) 2015年 张鸿峰. All rights reserved.
//

#import "MyActivityViewController.h"

#import "ActivityStore.h"

#import "DataBaseSingleTon.h"

#import "ActivityDetailViewController.h"

@interface MyActivityViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) UITableView *myTabelView;

@property (nonatomic,retain) NSMutableArray *arrM;

@property (nonatomic,retain) ActivityList *activityList;


@end

@implementation MyActivityViewController

- (void)dealloc
{
    [_myTabelView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"我的活动收藏";

    [self.myTabelView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

    
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName1"];
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM activityStore WHERE userName = '%@'",isLogin];
    
    self.arrM = [[DataBaseSingleTon shareDataBase] selectActivityStoreSQL:sql];
}


- (UITableView *)myTabelView
{
    if (_myTabelView == nil) {
        
        self.myTabelView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
        self.myTabelView.delegate = self;
        self.myTabelView.dataSource = self;
        [self.view addSubview:self.myTabelView];
        [self.myTabelView release];
    }
    return _myTabelView;
}

//删除活动收藏
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        ActivityStore *activityStore = [self.arrM objectAtIndex:indexPath.row];
        NSString *sql = [NSString stringWithFormat:@"DELETE FROM activityStore WHERE title = '%@' AND userName = '%@'", activityStore.title, activityStore.userName];
        
        [[DataBaseSingleTon shareDataBase] deleteActivity:sql];
        
        [self.arrM removeObjectAtIndex:indexPath.row];
        [self.myTabelView reloadData];
        
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [self.myTabelView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    ActivityStore *activityStore = [self.arrM objectAtIndex:indexPath.row];
    cell.textLabel.text = activityStore.title;
    
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
